#[allow(non_snake_case)]
pub mod K;
#[allow(non_snake_case)]
pub mod LUA;

mod cid;
pub use cid::CID;

mod kid;
pub use kid::KID;

pub mod exchange;
pub mod stripe;
pub use api::Api;
pub use bill_month::bill_month;
mod api;
mod bill_month;

use r::fred::interfaces::SetsInterface;

pub const BASE: f64 = 100.0;

pub async fn topup_f64(cid: u16, kid: u64, rid: u64, uid: u64, amount: f64) -> anyhow::Result<f64> {
  let amount = (amount * BASE).round();
  Ok(topup_cent(cid, kid, rid, uid, amount as _).await? as f64 / BASE)
}

pub async fn topup_cent(
  cid: u16,
  kid: u64,
  rid: u64,
  uid: u64,
  amount: i64,
) -> anyhow::Result<i64> {
  let ts = sts::sec();
  let remain = m::payBillNew!(cid, kid, rid, uid, amount, ts);
  if remain > 0 {
    r::R.sadd(K::PAY_ON, intbin::u64_bin(uid)).await?;
  }
  Ok(remain)
}

pub async fn month_bill(uid: u64, month: i32) -> anyhow::Result<Vec<(u16, u64, u64, i32, i64)>> {
  let mday = sts::month_day(month);
  Ok(m::q!(format!(
    "CALL payBill({uid},{},{})",
    mday.start, mday.end
  )))
}
